package com.data.modules.smzh.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.data.common.constant.MsgConstants;
import com.data.common.util.KamliClientUtil;
import com.data.modules.smzh.entity.Facedevice;
import com.data.modules.smzh.entity.SmUpgradeLog;
import com.data.modules.smzh.mapper.SmUpgradeLogMapper;
import com.data.modules.smzh.service.IBuildingService;
import com.data.modules.smzh.service.IFacedeviceService;
import com.data.modules.smzh.service.ISmUpgradeLogService;
import com.data.modules.smzh.entity.SmUpgradeLog;
import com.data.modules.smzh.mapper.SmUpgradeLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * @Description: 版本升级操作
 * @Author: data-boot
 * @Date:   2023-03-24
 * @Version: V1.0
 */
@Service
@Slf4j
public class SmUpgradeLogServiceImpl extends ServiceImpl<SmUpgradeLogMapper, SmUpgradeLog> implements ISmUpgradeLogService {


    @Autowired
    private IBuildingService buildingService;
    @Autowired
    private IFacedeviceService facedeviceService;
    //其他设备
    private final static String stringOtherType = "SMW-721A10,SMW-721M";

    @Override
    public void pushUpMsg(String id) {

        SmUpgradeLog announcement = this.getById(id);

        String cmd = MsgConstants.UPGRADE_MSG;
        List<Map> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        if(ObjectUtil.isNotEmpty(announcement.getUrl())){
            cmd = MsgConstants.UPGRADE_VERSION_MSG;

            map.put("model",announcement.getModel());
            map.put("url",announcement.getUrl());
            list.add(map);
        }else{
            // map.put("content","");
            list.add(map);
        }

        if(stringOtherType.contains(announcement.getModel())){//其他设备
            if(ObjectUtil.isEmpty(announcement.getUniqueNo())){
                //通过设备类型 查询项目所属下的设备

                Page<Facedevice> page = new Page<Facedevice>(1, 100000);

                Facedevice facedevice = new Facedevice();
                facedevice.setModel(announcement.getModel());
                facedevice.setProjectId(announcement.getProjectNo());

                IPage<Facedevice> pageList = facedeviceService.pageList(page, facedevice);
                String finalCmd1 = cmd;
                pageList.getRecords().stream().forEach(x->{

                    KamliClientUtil.doPostpushMsgUr(x.getSerialNo(), finalCmd1,list);
                });

            }else{
                KamliClientUtil.doPostpushMsgUr(announcement.getUniqueNo(), cmd,list);
            }

        }else {
            if(ObjectUtil.isEmpty(announcement.getUniqueNo())){// 室内机

                //通过项目主键查询出 绑定的 设备序列号推送消息
                List<String> serialNoList = buildingService.queryDeviceInfoByProjectId(announcement.getProjectNo());

                String finalCmd = cmd;
                serialNoList.parallelStream().forEach(x->{

                    Boolean flag=true;
                    flag = KamliClientUtil.doPostpushMsgUr(x, finalCmd,list);
                    log.info("推送消息序列号{},结果：{}",x,flag);
                });
            }else{
                KamliClientUtil.doPostpushMsgUr(announcement.getUniqueNo(), cmd,list);
            }
        }

    }
}
